package jack.code.hnzb.rule;

import jack.code.hnzb.utils.RedisUtil;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 *  搜索规则
 * Created by LiJiakui on 17/5/2.
 */
public class SearchRule {
    //搜索范围:title 标题,body内容
    private static final String range = "body";
    //包含
    private String contain = "";
    //例外
    private String except = "";

    /**
     * 湖南招标监管网 检索条件
     */
    private static List<String> biddingRules = new ArrayList<>();
    static {
        biddingRules.add("医院,卫生|废水,污水");
        biddingRules.add("活性氧");
        biddingRules.add("消毒粉");
        biddingRules.add("中央空调|清洗,水处理,净化,清洁,消毒");
        biddingRules.add("空气净化");
    }

    /**
     * 湖南招标监管网
     * 检索内容,判断是否满足规则
     * @return
     */
    public static String searchBidding(String data){
        for(String rules : biddingRules){
            StringBuffer sb = new StringBuffer("");
            boolean b = false;
            //当前规则的多个分组
            String[] groups = rules.split("[|]");
            for(String group : groups){
                //当前规则组中,每组的规则词
                //如果每组中的规则词全部都不满足,则直接结束次组的检索,进行下一条检索
                b = false;
                String[] terms = group.split(",");
                for(String term : terms){
                    if(data.contains(term)){
                        if(sb.toString().equals("")){
                            sb.append(term);
                        }else{
                            sb.append("+"+term);
                        }
                        b = true;
                        break;
                    }
                }
                //如果当前其中一组的规则未能出现命中,则本次检索失败,直接跳出
                if(!b){
                    break;
                }
            }
            //如果满足其中一组规则,则命中成功,返回true
            if(b){
                return sb.toString();
            }
        }

        return "";
    }


    /**
     * 获取检索范围 body:全文,title:标题
     * @return
     */
    public static String range(){
        try {
            String str = RedisUtil.getJedisVal("range");
            if(!StringUtils.isEmpty(str)){
                return str;
            }else{
                RedisUtil.setJedisVal("range",range);
                return range;
            }
        } catch (Exception e) {
            return range;
        }
    }
}
